package Kuaishou;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import scala.Int;

public class Test18遍历方式的效率 {
    public static void main(String[] args) {
        int size = 5000000;
        long start0 = System.currentTimeMillis();
        Map<Long, Long> map = new TreeMap<>();
        List<Long> list = new ArrayList<>(size);
        for (long i = 0; i < size; i++) {
            list.add(i + 1);
            map.put(i, i);
        }
        System.out.println(map.size());
        System.out.println(System.currentTimeMillis() - start0);

        long start1 = System.currentTimeMillis();
        System.out.println(sum1(list));
//        System.out.println(map.size());
        System.out.println(System.currentTimeMillis() - start1);

        long start2 = System.currentTimeMillis();
        System.out.println(sum2(list));
        System.out.println(System.currentTimeMillis() - start2);

        long start3 = System.currentTimeMillis();
        System.out.println(sum3(list));
        System.out.println(System.currentTimeMillis() - start3);

        long start4 = System.currentTimeMillis();
        System.out.println(sum4(list));
        System.out.println(System.currentTimeMillis() - start4);
    }

    private static long sum1(List<Long> list) {
        long sum = 0;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            sum += list.get(i);
//            sum += new BigDecimal(list.get(i) * 0.1).setScale(0 , BigDecimal.ROUND_HALF_UP).longValue();
//            sum += (long)(list.get(i) * 0.1);
//            sum += Math.round(list.get(i) * 0.1);
        }
        return sum;
    }

    private static long sum2(List<Long> list) {
        long sum = 0;
        for (long num : list) {
            sum += num;
        }
        return sum;
    }

    private static long sum3(List<Long> list) {
        return list.stream().reduce(0L, Long::sum);
    }

    private static long sum4(List<Long> list) {
        long sum = 0;
        Iterator<Long> iterator = list.iterator();
        while (iterator.hasNext()) {
            sum += iterator.next();
        }
        return sum;
    }

}
